#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.sympy import *

bignum = 50
n_iter = 0
def rand():
    return One * oneof(1, 2, 3, 4, 5, 6, 8, 9, 12, 18) / 6

def test_OK(n):
    a, b = n.as_numer_denom()
    if a < sqrt(2 * bignum) and b < bignum:
        return True
    else:
        return False

iter = 0
while True:
    # parâmetros iniciais
    with display_block('Entradas'):
        A = oneof(Half, One, 3 * Half, Two)
        B = rand()
        w = rand()
        lambd = rand()
        wt = w * t
        Bt = B * t
        Rt = A * exp(-Bt)

    with display_block('Resultados, comprimento'):
        correta = A * sqrt(B ** 2 + w ** 2) / B
        semModulo = -A
        semModuloNeg = A
        semDerivada = A / B
        semDerivadaModulo = A * (B + w) / (B * B + w * w)
        derivadaErrada = semDerivada * w

    with display_block('Resultados, comprimento'):
        Icorreta = lambd * correta * A ** 2 / 3
        Idensidade = correta * A ** 2 / 3
        IsemModulo = -lambd * A ** 3 * (3 * B ** 2 + w ** 2 - 2 * B * w) / (9 * B ** 2 + w ** 2)
        IsemModuloNeg = lambd * A ** 3 * (3 * B ** 2 + w ** 2 - 2 * B * w) / (9 * B ** 2 + w ** 2)
        IsemModuloDensidade = A ** 3 * (3 * B ** 2 + w ** 2 - 2 * B * w) / (9 * B ** 2 + w ** 2)
        IsemDerivada = lambd * A ** 3 / B / 3
        IsemDerivadaDensidade = A ** 3 / B / 3
        IsemDerivadaModulo = A ** 3 * (3 * B + w) / (9 * B ** 2 + w ** 2)

    # Distratores
    respsS = set([correta, semModulo, semDerivada, semDerivadaModulo, derivadaErrada])
    respsI = set([Icorreta, Idensidade, IsemModulo, IsemModuloDensidade, IsemDerivada, IsemDerivadaDensidade, IsemDerivadaModulo])
    resps = respsS.union(respsI)
    distr = set([])
    while len(distr) < 3:
        aux = oneof(*respsS)
        aux *= rand()
        if aux not in respsS:
            distr.add(aux)

    with display_block('Distratores'):
        dis1, dis2, dis3 = distr

    # Test complexity of numbers
    iter += 1

    if len(respsS) < 5 or len(respsI) < 7:
        print 'Repetidas...'
        continue

    if all(test_OK(x) for x in resps):
        break

print 'Número de iterações: ', iter
